#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_04_test.py
@Time    :   2025/04/07 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
import pytest
import logging
import time
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
import random
import datetime

'''故障快照01,检测同一点火周期内故障重复产生快照信息应不更新'''
def snapshot_01(Driver, iStep, strFun = 'UDS'):
   # 设置电压为8v,生成低压故障
   iStep = Driver['CAN'].SetVolt(Driver, 8.0, 3, iStep)

   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 09 01 04', 0, iStep, False, strFun, 'noClearFault')
   snapshot_01 = uds_resp[7:]
   iStep = Driver['CAN'].SetVolt(Driver, 12, 3, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 08 01 04', 0, iStep, False, strFun, 'noClearFault')

   # 设置车速>3kph = 80kph
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 80)
   # 设置电压为8v,生成低压故障
   iStep = Driver['CAN'].SetVolt(Driver, 8.1, 3, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 09 01 04', 0, iStep, False, strFun, 'noClearFault')
   with allure.step(f"Step{iStep}: Determine if the snapshot_01 is the same as before # snapshot_01 = {snapshot_01}"):
      with assume: assert uds_resp[7:] == snapshot_01
      iStep += 1

   # 设置车速>3kph = 80kph
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  0)
   iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDS', 'noClearFault')

   return iStep

'''故障快照05,检测同一点火周期内故障重复产生快照信息应更新'''
def snapshot_05(Driver, iStep, strFun = "UDS"):

   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 20)
   # 设置电压为8v,生成低压故障
   iStep = Driver['CAN'].SetVolt(Driver, 8, 3, iStep)

   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 09 05', 0, iStep, False, strFun, 'noClearFault')
   snapshot_05 = uds_resp[7:]
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 09 05', 0, iStep, False, strFun, 'noClearFault')

   iStep = Driver['CAN'].SetVolt(Driver, 12, 7, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 08 05', 0, iStep, False, strFun, 'noClearFault')

   # 设置车速>3kph = 80kph
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 80)

   # 设置电压为8v,生成低压故障
   iStep = Driver['CAN'].SetVolt(Driver, 8.1, 7, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 09 05', 0, iStep, False, strFun, 'noClearFault')
      
   with allure.step(f"Step{iStep}: Determine if the snapshot_05 is not the same as before # NA"):
      with assume: assert uds_resp[7:] != snapshot_05
      iStep += 1

   # 设置车速 = 0kph
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  0)
   iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDS', 'noClearFault')

   return iStep

'''故障快照FF,同时返回01与05快照数据'''
def snapshot_FF(Driver, iStep, UDSAdress='UDS'):

   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 20)
   # 设置电压为8v,生成低压故障，生成故障快照01
   iStep = Driver['CAN'].SetVolt(Driver, 8, 3, iStep)
   # 读取故障快照01
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 09 01 04', 0, iStep, False, UDSAdress, 'noClearFault')
   snapshot_01_data = uds_resp[6:]
   # 恢复故障
   iStep = Driver['CAN'].SetVolt(Driver, 12, 7, iStep)
   

   # 设置电压为8.5v,再次生成低压故障，生成故障快照05
   iStep = Driver['CAN'].SetVolt(Driver, 8.5, 7, iStep)
   # 读取故障快照05
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 09 05 04', 0, iStep, False, UDSAdress, 'noClearFault')
   snapshot_05_data = uds_resp[6:]
   # 恢复故障
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  0)
   iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)

   # 读取故障快照FF
   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 FF', '59 04 91 17 16 09 04', 0, iStep, False, UDSAdress, 'noClearFault')
   snapshot_FF_data = uds_resp[6:]
   with allure.step(f"Step{iStep}: 判断FF是否读取到全部快照信息 # 包含01快照与05快照"):
      with assume: assert snapshot_FF_data == snapshot_01_data + snapshot_05_data, logging.error("FF读取快照信息不全")
      iStep += 1

   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep)

   return iStep

def checkSnapShotData(iStep, SnapShot_data, voltage, vspeed, distance, year, month, day, hour, minute, second):
   # 判断第一组数据标识符data identifier
   with allure.step(f"Step{iStep}: 判断第一组数据标识符 # data_identifier = 0xCF00"):
      with assume: assert SnapShot_data[0:2] == [0xCF,0x00], logging.error("数据标识符data identifier 错误,应该是0xCF00,实际是0x{:02X}{:02X}".format(SnapShot_data[0],SnapShot_data[1]))
      iStep += 1
   # 判断电压是否正确
   snapshot_Voltage = SnapShot_data[2]/10
   with allure.step(f"Step{iStep}: 判断第一组电压数据 #  {voltage - 0.5}< {snapshot_Voltage} < {voltage + 0.5}"):
      with assume: assert snapshot_Voltage >= voltage - 0.5 and snapshot_Voltage <= voltage + 0.5, logging.error(f"电压数据错误,应该是{voltage - 0.5} < {snapshot_Voltage} < {voltage + 0.5}")
      iStep += 1

   # 判断第二组数据标识符
   with allure.step(f"Step{iStep}: 判断第二组数据标识符 # data_identifier = 0xCF01"):
      with assume: assert SnapShot_data[3:5] == [0xCF,0x01], logging.error("数据标识符data identifier 错误,应该是0xCF02,实际是0x{:02X}{:02X}".format(SnapShot_data[3],SnapShot_data[4]))
      iStep += 1
   # 判断车速是否正确
   snapshot_vspeed = int.from_bytes(SnapShot_data[5:7], byteorder='big') * 0.125
   with allure.step(f"Step{iStep}: 判断第二组车速数据 # {vspeed - 0.5} < {snapshot_vspeed} < {vspeed + 0.5}"):
      with assume: assert vspeed - 0.5 < snapshot_vspeed < vspeed + 0.5, logging.error(f"车速数据错误,应该是{vspeed - 0.5} < {snapshot_vspeed} < {vspeed + 0.5}")
      iStep += 1
      
   # 判断第三组数据标识符
   with allure.step(f"Step{iStep}: 判断第三组数据标识符 # data_identifier = 0xCF03"):
      with assume: assert SnapShot_data[7:9] == [0xCF,0x03], logging.error("数据标识符data identifier 错误,应该是0xCF03,实际是0x{:02X}{:02X}".format(SnapShot_data[6],SnapShot_data[7]))
      iStep += 1
   # 判断总里程是否正确
   snapshot_distance = int.from_bytes(SnapShot_data[9:12], byteorder='big')
   with allure.step(f"Step{iStep}: 判断第三组总里程数据 # {distance - 0.5} < {snapshot_distance} < {distance + 0.5}"):
      with assume: assert distance - 0.5 < snapshot_distance < distance + 0.5, logging.error(f"总里程数据错误,应该是{distance - 0.5} < {snapshot_distance} < {distance + 0.5}")
      iStep += 1

   # 判断第四组数据标识符
   with allure.step(f"Step{iStep}: 判断第四组数据标识符 # data_identifier = 0xCF05"):
      with assume: assert SnapShot_data[12:14] == [0xCF,0x05], logging.error("数据标识符data identifier 错误,应该是0xCF05,实际是0x{:02X}{:02X}".format(SnapShot_data[12],SnapShot_data[13]))
      iStep += 1
   with allure.step(f"Step{iStep}: 判断第四组时间数据 # {year-2000} {month} {day} {hour} {minute} {second}"):
      with assume: assert SnapShot_data[14:20] == [year-2000, month, day, hour, minute, second], logging.error(f"时间数据错误,应该是{year-2000} {month} {day} {hour} {minute} {second}")
      iStep += 1

'''检测故障快照01信息是否正确'''
def checkSnapShot_01(Driver, iStep, UDSAdress = 'UDS'):
   # 设置车速
   vspeed = random.randint(30, 150)
   timeNow = datetime.datetime.now()
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  vspeed)
   #设置年
   Driver['CAN'].set_signal_Year(timeNow.year)
   #设置月
   Driver['CAN'].set_signal_Month(timeNow.month)
   #设置日
   Driver['CAN'].set_signal_Day(timeNow.day)    
   #设置时
   Driver['CAN'].set_signal_Hour(timeNow.hour)     
   #设置分
   Driver['CAN'].set_signal_Minute(timeNow.minute)
   #设置秒
   Driver['CAN'].set_signal_Second(timeNow.second)
   #设置总里程
   distance = random.randint(20, 1200)
   Driver['CAN'].set_signal_Total_Distance(distance)
   time.sleep(2)

   # 第一次生成故障
   logging.info("第一次生成故障")
   # 设置电压
   voltage = random.randint(170, 190)/10
   Driver['CAN'].SetVolt(Driver, voltage, 10, iStep)

   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 17 01', '59 04 91 17 17 09 01 04', 0, iStep, False, UDSAdress, 'noClearFault')
   SnapShot_01 = uds_resp[8:]
   
   checkSnapShotData(iStep,SnapShot_01, voltage, vspeed, distance, timeNow.year, timeNow.month, timeNow.day, timeNow.hour, timeNow.minute, timeNow.second)

   # 恢复
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  0)
   iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep)

   '''检测故障快照01信息是否正确'''
def checkSnapShot_05(Driver, iStep, UDSAdress = 'UDS'):
      # 设置车速
   vspeed = random.randint(30, 150)
   timeNow = datetime.datetime.now()
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  vspeed)
   #设置年
   Driver['CAN'].set_signal_Year(timeNow.year)
   #设置月
   Driver['CAN'].set_signal_Month(timeNow.month)
   #设置日
   Driver['CAN'].set_signal_Day(timeNow.day) 
   #设置时
   Driver['CAN'].set_signal_Hour(timeNow.hour)     
   #设置分
   Driver['CAN'].set_signal_Minute(timeNow.minute)
   #设置秒
   Driver['CAN'].set_signal_Second(timeNow.second)
   #设置总里程
   distance = random.randint(20, 1200)
   Driver['CAN'].set_signal_Total_Distance(distance)
   time.sleep(2)

   # 第一次生成故障
   logging.info("第一次生成故障")
   # 设置电压
   voltage = random.randint(80, 90)/10
   Driver['CAN'].SetVolt(Driver, voltage, 10, iStep)

   iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 09 05 04', 0, iStep, False, UDSAdress, 'noClearFault')
   snapshot_05 = uds_resp[8:]
   
   checkSnapShotData(iStep,snapshot_05, vspeed, voltage, distance, timeNow.year, timeNow.month, timeNow.day, timeNow.hour, timeNow.minute, timeNow.second)

   # 恢复
   iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep,  0)
   iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep)
   

class Test_APP_DTCSnapshotRecordNUM_phy(object):

   '''NO Fault'''
   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-读取DTC快照信息-01')
   @allure.description('测试物理寻址默认会话下的读取DTC快照信息-01')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_01_No_Fault_Default_Phy(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 01"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'NoClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-读取DTC快照信息-01')
   @allure.description('测试物理寻址扩展会话下的读取DTC快照信息-01')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_01_No_Fault_extended_Phy(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 01"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-读取DTC快照信息-05')
   @allure.description('测试物理寻址默认会话下的读取DTC快照信息-05')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_05_No_Fault_Default_Phy(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 05"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'NoClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-读取DTC快照信息-05')
   @allure.description('测试物理寻址扩展会话下的读取DTC快照信息-05')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_05_No_Fault_extended_Phy(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 05"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   
   # ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   # @allure.severity('normal')
   # @allure.title('物理寻址-默认会话-读取DTC快照信息-FF')
   # @allure.description('测试物理寻址默认会话下的读取DTC快照信息-FF')
   # @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   # def test_DTCSnapshotRecordNUM_FF_No_Fault_Default_Phy(self, action,  Driver, dtcNumber,dtcName):
   #    iStep = 1
   #    req =  [0x19,0x04] + dtcNumber + [0xFF]

   #    if dtcNumber == [0xC2,0x6C,0x87]:
   #       resp = [0x59,0x04] + dtcNumber
   #    else:
   #       resp = [0x59,0x04] + dtcNumber + [0x00]
   #    logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')
   #    if uds_resp[5] != 0x00:
   #       with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   #       with assume: assert uds_resp[20:22] == [0x05,0x04], logging.error(f"0x{uds_resp[20]:02X}{uds_resp[21]:02X} != 0x0504")

   # ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName  in Set_up.DTC_list]
   # @allure.severity('normal')
   # @allure.title('物理寻址-扩展会话-读取DTC快照信息-FF')
   # @allure.description('测试物理寻址扩展会话下的读取DTC快照信息-FF')
   # @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   # def test_DTCSnapshotRecordNUM_FF_No_Fault_extended_Phy(self, action,  Driver, dtcNumber,dtcName):
   #    iStep = 1
   #    req =  [0x19,0x04] + dtcNumber + [0xFF]
   #    if dtcNumber == [0xC2,0x6C,0x87]:
   #       resp = [0x59,0x04] + dtcNumber
   #    else:
   #       resp = [0x59,0x04] + dtcNumber + [0x00]
   #    logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
   #    if uds_resp[5] != 0x00:
   #       with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   #       with assume: assert uds_resp[20:22] == [0x05,0x04], logging.error(f"0x{uds_resp[20]:02X}{uds_resp[21]:02X} != 0x0504")


   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-故障快照01,检测同一点火周期内故障重复产生快照信息应不更新')
   @allure.description('测试物理寻址默认会话下的故障快照01,检测同一点火周期内故障重复产生快照信息应不更新')
   def test_DTCSnapshotRecordByDTCNumber_01_Default_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 1, iStep, False, 'UDS', 'ClearFault')
      snapshot_01(Driver, iStep)
      

      
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-故障快照01,检测同一点火周期内故障重复产生快照信息应不更新')
   @allure.description('测试物理寻址扩展会话下的故障快照01,检测同一点火周期内故障重复产生快照信息应不更新')
   def test_DTCSnapshotRecordByDTCNumber_01_Extended_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 3, iStep, False, 'UDS', 'ClearFault')
      snapshot_01(Driver, iStep)

   
   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-故障快照05,检测同一点火周期内故障重复产生快照信息应更新')
   @allure.description('测试物理寻址默认会话下的故障快照05,检测同一点火周期内故障重复产生快照信息应更新')
   def test_DTCSnapshotRecordByDTCNumber_05_Default_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 1, iStep, False, 'UDS', 'ClearFault')
      snapshot_05(Driver, iStep)
      

      
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-故障快照05,检测同一点火周期内故障重复产生快照信息应更新')
   @allure.description('测试物理寻址扩展会话下的故障快照05,检测同一点火周期内故障重复产生快照信息应更新')
   def test_DTCSnapshotRecordByDTCNumber_05_Extended_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 3, iStep, False, 'UDS', 'ClearFault')
      snapshot_05(Driver, iStep)


   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-故障快照01,检测快照数据是否正确')
   @allure.description('测试物理寻址默认会话下的故障快照01,检测快照数据是否正确')
   def test_checkSnapShot_01_Default_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 17 01', '59 04 91 17 17 00', 1, iStep, False, 'UDS', 'ClearFault')

      checkSnapShot_01(Driver, iStep)

   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-故障快照05,检测快照数据是否正确')
   @allure.description('测试物理寻址扩展会话下的故障快照05,检测快照数据是否正确')
   def test_checkSnapShot_05_Extended_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 3, iStep, False, 'UDS', 'ClearFault')
      checkSnapShot_05(Driver, iStep)

   # @allure.severity('normal')
   # @allure.title('物理寻址-默认会话-生成故障后读取DTC快照信息-FF')
   # @allure.description('测试物理寻址默认会话下的生成故障后读取DTC快照信息-FF')
   # def test_DTCSnapshotRecordByDTCNumber_FF_Default_Phy(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 FF', '59 04 91 17 16 00', 1, iStep, False, 'UDS', 'ClearFault')
   #    snapshot_FF(Driver, iStep)

   # @allure.severity('normal')
   # @allure.title('物理寻址-扩展会话-生成故障后读取DTC快照信息-FF')
   # @allure.description('测试物理寻址扩展会话下的生成故障后读取DTC快照信息-FF')
   # def test_DTCSnapshotRecordByDTCNumber_FF_Extended_Phy(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 FF', '59 04 91 17 16 00', 1, iStep, False, 'UDS', 'ClearFault')
   #    snapshot_FF(Driver, iStep)



class Test_APP_DTCSnapshotRecordNUM_Func(object):

   '''NO Fault'''
   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-读取DTC快照信息-01')
   @allure.description('测试功能寻址默认会话下的读取DTC快照信息-01')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_01_No_Fault_Default_Func(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 01"
      resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'NoClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-读取DTC快照信息-01')
   @allure.description('测试功能寻址扩展会话下的读取DTC快照信息-01')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_01_No_Fault_extended_Func(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 01"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

      
   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-读取DTC快照信息-05')
   @allure.description('测试功能寻址默认会话下的读取DTC快照信息-05')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_05_No_Fault_Default_Func(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 05"
      resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'NoClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")

   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-读取DTC快照信息-05')
   @allure.description('测试功能寻址扩展会话下的读取DTC快照信息-05')
   @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_05_No_Fault_extended_Func(self, action,  Driver, dtcNumber,dtcName):
      iStep = 1
      req = f"19 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 05"
      if dtcNumber == [0xC2,0x6C,0x87]:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)}"
      else:
         resp = f"59 04 {' '.join(f'{b:02X}' for b in dtcNumber)} 00"
      logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
      if uds_resp[5] != 0x00:
         with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   
   # ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName in Set_up.DTC_list]
   # @allure.severity('normal')
   # @allure.title('功能寻址-默认会话-读取DTC快照信息-FF')
   # @allure.description('测试功能寻址默认会话下的读取DTC快照信息-FF')
   # @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   # def test_DTCSnapshotRecordNUM_FF_No_Fault_Default_Func(self, action,  Driver, dtcNumber,dtcName):
   #    iStep = 1
   #    req =  [0x19,0x04] + dtcNumber + [0xFF]

   #    if dtcNumber == [0xC2,0x6C,0x87]:
   #       resp = [0x59,0x04] + dtcNumber
   #    else:
   #       resp = [0x59,0x04] + dtcNumber + [0x00]
   #    logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'ClearFault')
   #    if uds_resp[5] != 0x00:
   #       with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   #       with assume: assert uds_resp[20:22] == [0x02,0x04], logging.error(f"0x{uds_resp[20]:02X}{uds_resp[21]:02X} != 0x0204")

   # ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' ')) for dtcNumber, dtcName  in Set_up.DTC_list]
   # @allure.severity('normal')
   # @allure.title('功能寻址-扩展会话-读取DTC快照信息-FF')
   # @allure.description('测试功能寻址扩展会话下的读取DTC快照信息-FF')
   # @pytest.mark.parametrize('dtcNumber, dtcName', Set_up.DTC_list, ids=ids)
   # def test_DTCSnapshotRecordNUM_FF_No_Fault_extended_Func(self, action,  Driver, dtcNumber,dtcName):
   #    iStep = 1
   #    req =  [0x19,0x04] + dtcNumber + [0xFF]
   #    if dtcNumber == [0xC2,0x6C,0x87]:
   #       resp = [0x59,0x04] + dtcNumber
   #    else:
   #       resp = [0x59,0x04] + dtcNumber + [0x00]
   #    logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
   #    if uds_resp[5] != 0x00:
   #       with assume: assert uds_resp[6:8] == [0x01,0x04], logging.error(f"0x{uds_resp[6]:02X}{uds_resp[7]:02X} != 0x0104")
   #       with assume: assert uds_resp[20:22] == [0x02,0x04], logging.error(f"0x{uds_resp[20]:02X}{uds_resp[21]:02X} != 0x0204")


   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-同一点火周期内故障重复生成DTC快照信息-01应不更新')
   @allure.description('测试功能寻址默认会话下的同一点火周期内故障重复生成DTC快照信息-01应不更新')
   def test_DTCSnapshotRecordByDTCNumber_01_Default_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 1, iStep, False, 'UDSFun', 'ClearFault')
      snapshot_01(Driver, iStep, 'UDSFun')
      

      
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-同一点火周期内故障重复生成DTC快照信息01应不更新')
   @allure.description('测试功能寻址扩展会话下的同一点火周期内故障重复生成DTC快照信息-01应不更新')
   def test_DTCSnapshotRecordByDTCNumber_01_Extended_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 3, iStep, False, 'UDSFun', 'ClearFault')
      snapshot_01(Driver, iStep, 'UDSFun')

   
   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-同一点火周期内故障重复生成DTC快照信息05应更新')
   @allure.description('测试功能寻址默认会话下的同一点火周期内故障重复生成DTC快照信息-05应更新')
   def test_DTCSnapshotRecordByDTCNumber_05_Default_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 1, iStep, False, 'UDSFun', 'ClearFault')
      snapshot_05(Driver, iStep, "UDSFun")
      

      
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-同一点火周期内故障重复生成DTC快照信息05应更新')
   @allure.description('测试功能寻址扩展会话下的同一点火周期内故障重复生成DTC快照信息-05应更新')
   def test_DTCSnapshotRecordByDTCNumber_05_Extended_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 3, iStep, False, 'UDSFun', 'ClearFault')
      snapshot_05(Driver, iStep, 'UDSFun')

   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-故障快照01,检测快照数据是否正确')
   @allure.description('测试功能寻址默认会话下的故障快照01,检测快照数据是否正确')
   def test_checkSnapShot_01_Default_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 17 01', '59 04 91 17 17 00', 1, iStep, False, 'UDSFun', 'ClearFault')
      checkSnapShot_01(Driver, iStep, 'UDSFun')

   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-故障快照05,检测快照数据是否正确')
   @allure.description('测试功能寻址扩展会话下的故障快照05,检测快照数据是否正确')
   def test_checkSnapShot_05_Extended_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 05', '59 04 91 17 16 00', 3, iStep, False, 'UDSFun', 'ClearFault')
      checkSnapShot_05(Driver, iStep, 'UDSFun')


   # @allure.severity('normal')
   # @allure.title('功能寻址-默认会话-生成故障后读取DTC快照信息-FF')
   # @allure.description('测试功能寻址默认会话下的生成故障后读取DTC快照信息-FF')
   # def test_DTCSnapshotRecordByDTCNumber_FF_Default_Func(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 1, iStep, False, 'UDSFun', 'ClearFault')
   #    snapshot_FF(Driver, iStep, 'UDSFun')

   # @allure.severity('normal')
   # @allure.title('功能寻址-扩展会话-生成故障后读取DTC快照信息-FF')
   # @allure.description('测试功能寻址扩展会话下的生成故障后读取DTC快照信息-FF')
   # def test_DTCSnapshotRecordByDTCNumber_FF_Extended_Func(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, '19 04 91 17 16 01', '59 04 91 17 16 00', 3, iStep, False, 'UDSFun', 'ClearFault')
   #    snapshot_FF(Driver, iStep, 'UDSFun')

